import { isMobile } from "@/util/validator.js";
import { getTenantActivityInfo,joinActivity } from '@/util/api/activity.js'
import { debounce } from "@/util/debounce.js";
import { companyList } from "@/util/api/es.js";
import { isWeixin, wxShareConfig } from "util/wx.js";
export default {
  data() {
    return {
      userInfo: {},
      activityInfo: {},
      companyNameList: [], // 搜索结果
      activityId: '',
      form: {
        activityId: '',
        companyName: '',
        phoneNumber: '',
        userName: '',
        position: '',
        companySynopsis: '',
        type: '' //1报名 2签到
      },
      keyWord: '',
      showList: false,
      showtips: true, //显示跳转小程序按钮
      showNumber: false
    }
  },
  watch: {
    'form.companyName': debounce(function (e) {
      this.form.companyName = e.trim();
      if (e.trim() !== ""  && this.showList) {
        this.getCompanyName();
      } else {
        this.companyNameList = [];
      }
    }, 1000),
    'form.activityId'(val){
      if(val){
        this.getTenantActivityInfo()
      }
    }
  },
  onLoad(e) {
    this.form.type = e.type
    this.form.activityId = e.id
    this.getTenantActivityInfo()
  },
  methods: {
    // 监听input框的值
    chandleClick(type) {
      if(type === 'focus') {
        this.showList = true
      }else if(type === 'blur'){
        setTimeout(()=>{
          this.showList = false
        })
      }
    },
    // 获取企业名称
    getCompanyName() {
      let obj = {
        area: '',
        city: '',
        companyId: '',
        companyName: this.form.companyName,
        endLat: 0,
        endLng: 0,
        pageSize: 10,
        province: '',
        startLat: 0,
        startLng: 0,
      }
      companyList(obj).then(res => {
        let { retCode, data } = res
        if (retCode == 0 && data.content.length) {
          this.companyNameList = data.content
          this.$changeText(data.content, 'companyName', this.form.companyName)
        } else if (res.code == 10005) {
          uni.showModal({
            title: '提示',
            content: '企业查询数量已满',
            success: function (res) {
              if (res.confirm) {
                uni.navigateTo({
                  url: '/pages/user/openVip/Index',
                })
              }
            },
          })
        } else if (retCode == 0 && !data.content.length) {
          setTimeout(()=>{
            uni.showToast({
              title: "找不到您要查找的企业",
              duration: 2000,
              icon: "none"
            });
          },100)
          this.companyNameList = []
        }
      })
    },
    selectItem(val, id) {
      val = this.$matcheHTML(val) // 去除html标签
      this.form.companyName = val
      this.companyNameList = []
      this.showList = false
    },
    getTenantActivityInfo() {
      getTenantActivityInfo({id: this.form.activityId}).then(res=>{
        let obj = res.result
        for (const key in obj) {
          if ((obj[key]) ===null) delete obj[key];
        }
        this.form = Object.assign(this.form,obj)
        if (isWeixin()) {
          this.initShareConfig()
        }
      })
    },
    submitForm() {
      let arr = []
      arr = [
        { value: this.form.companyName, text: "请输入公司名称", type: 1 },
        { value: this.form.userName, text: "请输入姓名", type: 1 },
        { value: this.form.phoneNumber, text: "请输入正确的手机号码", type: 2 },
      ]
      for (let i = 0; i < arr.length; i++) {
        let obj = arr[i];
        if (obj.value === "") {
          uni.showToast({
            title: obj.text,
            duration: 2000,
            icon: "none"
          });
          return;
        }
        if (obj.type === 2 && !isMobile(obj.value)) {
          uni.showToast({
            title: obj.text,
            duration: 2000,
            icon: "none"
          });
          return;
        }
      }
      joinActivity(this.form).then(res=>{
        if(res.code === 200){
          uni.showModal({
            title: '提示',
            content: '提交成功！是否去创建并发布个人活动？',
            cancelText: "返回首页", // 取消按钮的文字 
            confirmText: "创建活动", // 确认按钮文字 
            confirmColor: '#1678ff',
            success: (res) => {
              if (res.confirm) {
                let token = this.$getToken();
                if (!token) {
                  uni.showToast({
                    title: "您还没有登录,登录后创建并发布活动！",
                    duration: 2000,
                    icon: "none"
                  });
                  uni.setStorageSync("pageUrl", window.location.origin + '/pages/activity/add/Index');
                  setTimeout(() => {
                    uni.redirectTo({
                      url: '/pages/login/Index'
                    });
                  }, 2000)
                }else{
                   uni.redirectTo({
                    url: '/pages/activity/add/Index?type=3',
                  })
                }
              }else{
                uni.switchTab({
                  url: "/pages/index/index",
                })
              }
            }
          })
        }else{
          uni.showToast({
            title: res.message,
            duration: 2000,
            icon: "none"
          });
        }
      })
    },
    onClose() {
      this.showtips = false
    },
    initShareConfig() {
      let windowurl = window.location.href.split('#')[0]
      uni.request({
        url: "https://oldm.51bmj.cn/WeiXin/GetClientSign", //仅为示例，并非真实接口地址。
        data: {
          rawUrl: windowurl
        },
        dataType: 'json',
        header: {
          'X-Requested-With': 'XMLHttpRequest',
        },
        method: 'GET',
        success: res => {
          if (res.statusCode == 200) {
            let { data } = res
            let shareInfo = {
              title: this.activityInfo.title,
              link: window.location.href
            }
            wxShareConfig(this.$wx, data, shareInfo)
          }
        },
      })
    },
  }
}